﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SMS_Server;

namespace SMS_Server_Manager.Forms
{
    public partial class ApplyDialogue : Form
    {
        public ApplyDialogue()
        {
            InitializeComponent();
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private bool SaveBack()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            if (path[path.Length - 1] == '\\') path = path.Substring(0, path.Length - 1);
            try
            {
                FileStream Sourcefs = new FileStream(path + "\\App_data\\ManageConfig.sdf", FileMode.Open, FileAccess.Read);
                FileStream Destinationfs = new FileStream(path + "\\config\\config.src", FileMode.Create, FileAccess.Write);

                int counter = 0;
                long totall = Sourcefs.Length;
                long tillnow = 0;
                byte[] buffer = new byte[1024];

                while (true)
                {
                    counter = Sourcefs.Read(buffer, 0, buffer.Length);
                    if (counter == 0) break;

                    if (counter == buffer.Length)
                    {
                        Destinationfs.Write(buffer, 0, counter);
                    }
                    else
                    {
                        byte[] secbuff = new byte[counter];
                        for (int i = 0; i < counter; i++)
                        {
                            secbuff[i] = buffer[i];
                        }
                        Destinationfs.Write(secbuff, 0, counter);
                    }
                    tillnow += counter;
                }
                Destinationfs.Close();
                Sourcefs.Close();
            }
            catch
            {
                return false;
            }
            return true;
        }

        private void ApplyAndRestart_Click(object sender, EventArgs e)
        {
            SaveBack();
            Global.unsaved_changes = false;
            if (SMSServiceController.Status != System.ServiceProcess.ServiceControllerStatus.Stopped)
                SMSServiceController.Stop();
            SMSServiceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
            SMSServiceController.Start();
            SMSServiceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void ApplyOnRestart_Click(object sender, EventArgs e)
        {
            SaveBack();
            Global.unsaved_changes = false;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }
}
